package veterinaria.servlet.cliente;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Cliente;
import veterinaria.negocio.GestionAdministrarClientes;

/**
 * Servlet implementation class ClienteBuscarServlet
 */
@WebServlet("/ClienteBuscarServlet")
public class ClienteBuscarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClienteBuscarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Dentro de doPost del servlet ClienteBuscarSerlvet");
		String x = request.getParameter("nombre");
		int y= Integer.parseInt(request.getParameter("cbofiltro"));
		GestionAdministrarClientes negocio = new GestionAdministrarClientes();
		try {
			switch (y) {
			case(0):
				Collection<Cliente> lista = negocio.buscarTPorNombre(x);
			// Guardar en el ambiente de request
			request.setAttribute("clientes", lista);
			RequestDispatcher rd = request.getRequestDispatcher("clienteBuscar.jsp");
			rd.forward(request, response);
			break;
			case(1):
				Collection<Cliente> listaP = negocio.buscarPorNombre(x, false);
			// Guardar en el ambiente de request
			request.setAttribute("clientes", listaP);
			RequestDispatcher rp = request.getRequestDispatcher("clienteBuscar.jsp");
			rp.forward(request, response);
			break;
			case(2):
				Collection<Cliente> listaC = negocio.buscarPorNombre(x, true);
			// Guardar en el ambiente de request
			request.setAttribute("clientes", listaC);
			RequestDispatcher rc = request.getRequestDispatcher("clienteBuscar.jsp");
			rc.forward(request, response);
			break;
			case(3):
			if (x == " ") {
				Collection<Cliente> listaT = negocio.buscarTPorNombre(x);
				// Guardar en el ambiente de request
				request.setAttribute("clientes", listaT);
				RequestDispatcher rt = request.getRequestDispatcher("clienteBuscar.jsp");
				rt.forward(request, response);
				} 
				else {			
					Collection<Cliente> listaT = negocio.buscarTPorNombre(x);
					// Guardar en el ambiente de request
					request.setAttribute("clientes", listaT);
					RequestDispatcher rt = request.getRequestDispatcher("clienteBuscar.jsp");
					rt.forward(request, response);
			}
			break;
			}
			
		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		 		
	}

}
